package com.rockcent.wechat.controller;

import com.aliyun.oss.OSSClient;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.Customer;
import com.rockcent.mall.lib.domain.CustomerDetail;
import com.rockcent.mall.lib.domain.UploadImageFile;
import com.rockcent.mall.lib.domain.enumclass.UploadFileBusinessType;
import com.rockcent.mall.lib.dto.CustomerDto;
import com.rockcent.mall.lib.dto.UploadImageFileDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.repository.CustomerDetailRepository;
import com.rockcent.mall.lib.repository.CustomerRepository;
import com.rockcent.mall.lib.repository.UploadImageFileRepository;
import com.rockcent.wechat.Prop;
import com.rockcent.wechat.Routes;
import com.rockcent.wechat.helper.CustomerHelper;
import org.apache.commons.lang3.EnumUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Date;

/**
 * Created by ISSUSER on 2016/11/11 0011.
 */
@RestController
@RequestMapping(value = Routes.API_PREFIX)
public class CustomerUploadHeadImgController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(CustomerUploadHeadImgController.class);

    @Autowired
    private CustomerHelper customerHelper;
    @Autowired
    private OSSClient ossClient;
    @Autowired
    private UploadImageFileRepository uploadImageFileRepository;
    @Autowired
    private Prop prop;
    @Autowired
    private CustomerRepository customerRepository;
    /**
     * @api{post} /api/v2/customer/detail/upload_head_img 更新用户头像
     * @apiGroup customer
     * @apiVersion 1.0.0
     *
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     *
     * @apiParam {File} file 用户头像文件
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
    "code": "1",
    "desc": "成功",
    "data": "http://uploads.rockcent.com//dev/customer/head_img/201611/1618714974d449d4de-887f-4e15-b723-7dd276c0efea1.jpg"
    }
     */
    @RequestMapping(value = Routes.CUSTOMER_DETAIL_UPLOAD_HEAD_IMG, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult uploadHeadImg(MultipartFile file, HttpServletRequest request) {
        CustomerDto customerDto = customerHelper.getSession(request);
        RestDoing doing = jr -> {

            if (file == null) {
                jr.errorParam("file 不能为空",log);
                return;
            }

            String headUrl = this.doUpload(file,  customerDto);
            jr.data = headUrl;

        };
        return doing.go(request, log);
    }

    private String doUpload(MultipartFile file, CustomerDto customerDto) throws IOException {
        // 文件名和存放文件名的位置
        UploadImageFile uploadImageFile = new UploadImageFile();
        String fileName = uploadImageFile.genFileName(file.getOriginalFilename());
        String allPath = uploadImageFile.genAllPath(prop.env, UploadFileBusinessType.CUSTOMER_HEAD_IMG, fileName);

        //更新顾客的头像地址信息
        Customer customer = customerRepository.findOne(customerDto.getId());
        customer.setWechatHeadimgurl(allPath);//存放头像的地址
        customer.setLastUpdated(new java.util.Date());
        customerRepository.save(customer);

        //存放文件
        ossClient.putObject(prop.aliyunOssBucketName, allPath, file.getInputStream());

        //返回地址
        String headUrl = prop.domainUploadsHttp + "/" + customer.getWechatHeadimgurl();
        return headUrl;
    }
}
